package{
	import com.PQLoad.PQLoader;
	import com.tools.Tip;
	
	import flash.display.Sprite;
	public class OutputParsing{
		static private var url:String;
		static private var outputList:Array=[];
		static private var resultList:Array=[];
		static private var fileNameList:Array=[];
		public function OutputParsing(){
			
		}
		static public function loadConfig(_url:String):void{
			url=_url;
			PQLoader.load(url,{onComplete:configLoaded});
		}
		static private function configLoaded(...args):void{
			var xml:XML=PQLoader.getXml("config/output.xml");
			for each(var ixml:XML in xml.item){
				outputList.push(ixml);
				resultList.push("");
				fileNameList.push(ixml.@name[0]);
			}
		}
		static public function traceConfig(panel:Sprite):void{
			var xmlHead:String="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n<data>\r\n";
			for(var s in resultList)resultList[s]=xmlHead;
			var config:String=xmlHead;
			var n:int=panel.numChildren;
			var err:String="";
			var tip:String="";
			for(var i:int=0;i<n;i++){
				try{
					var mc:Mc=panel.getChildAt(i) as Mc;
					if(mc.name.indexOf("instance")!=-1)continue;
					var res:String=(mc.url?mc.url.substr(mc.url.lastIndexOf("/")+1):"");
					config+="	<item x=\""+mc.x+"\" y=\""+mc.y+"\" width=\""+mc.width+"\" height=\""+mc.height+"\" scaleX=\""+mc.scaleX+"\" scaleY=\""+mc.scaleY+"\" rotation=\""+mc.rotation+"\" alpha=\""+mc.alpha+"\" type=\""+mc.type+"\" parent=\""+mc.parentName+"\" name=\""+mc.name+"\" res=\""+mc.url+"\"/>\r\n";
					for(var s in resultList){
						resultList[s]+=outputList[s]+"\r\n";
						while(resultList[s].indexOf("PQUIEdit_x")!=-1)resultList[s]=resultList[s].replace("PQUIEdit_x",mc.x);
						while(resultList[s].indexOf("PQUIEdit_y")!=-1)resultList[s]=resultList[s].replace("PQUIEdit_y",mc.y);
						while(resultList[s].indexOf("PQUIEdit_width")!=-1)resultList[s]=resultList[s].replace("PQUIEdit_width",mc.width);
						while(resultList[s].indexOf("PQUIEdit_height")!=-1)resultList[s]=resultList[s].replace("PQUIEdit_height",mc.height);
						while(resultList[s].indexOf("PQUIEdit_scaleX")!=-1)resultList[s]=resultList[s].replace("PQUIEdit_scaleX",mc.scaleX);
						while(resultList[s].indexOf("PQUIEdit_scaleY")!=-1)resultList[s]=resultList[s].replace("PQUIEdit_scaleY",mc.scaleY);
						while(resultList[s].indexOf("PQUIEdit_alpha")!=-1)resultList[s]=resultList[s].replace("PQUIEdit_alpha",mc.alpha);
						while(resultList[s].indexOf("PQUIEdit_rotation")!=-1)resultList[s]=resultList[s].replace("PQUIEdit_rotation",mc.rotation);
						while(resultList[s].indexOf("PQUIEdit_res")!=-1)resultList[s]=resultList[s].replace("PQUIEdit_res",res||"");
						while(resultList[s].indexOf("PQUIEdit_type")!=-1)resultList[s]=resultList[s].replace("PQUIEdit_type",mc.type);
						while(resultList[s].indexOf("PQUIEdit_parent")!=-1)resultList[s]=resultList[s].replace("PQUIEdit_parent",mc.parentName);
						while(resultList[s].indexOf("PQUIEdit_name")!=-1)resultList[s]=resultList[s].replace("PQUIEdit_name",mc.name);
					}
					tip="配置已生成成功！！！";
				}catch(e){
					err+=panel.getChildAt(i).name;
					var d:Date=new Date();
					FileHelper.createFileToExplorer("output/log_"+d.fullYear+d.month+d.date+d.hours+d.minutes+d.seconds+".txt",err);
					tip="配置生成失败，请查看output目录下的log日志！！！";
				}
			}
			config+="</data>";
			for(var s in resultList)resultList[s]+="</data>";
			FileHelper.createFileToApplicationDirectory("config/config.xml",config);
			n=fileNameList.length;
			for(var j:int=0;j<n;j++)FileHelper.createFileToApplicationDirectory("output/"+fileNameList[j],resultList[j]);
			Tip.showTip(tip);
		}
	}
}